package product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ChulList extends JInternalFrame {
	private JScrollPane scrollPane;
	HashMap<String, ChulGData> chulG;
	HashMap<String, MemberData> mem;

	DefaultTableModel model;
	TableColumn column = null;

	private JTable table;
	private JButton btnNewButton;
	private JButton button;
	private JTextField textField;
	private JButton btnClear;

	public ChulList(HashMap<String, ChulGData> chulG, HashMap<String, MemberData> mem) {
		super("출퇴근 리스트", true, true, true, false);
		this.chulG = chulG;

		this.mem = mem;
		Vector<String> colNames = new Vector<String>(); // 벡터에 테이블항목을 저장하기위해서 colNames 생성.						
		colNames.add("사원번호");
		colNames.add("이름");
		colNames.add("날짜");
		colNames.add("근태");

		model = new DefaultTableModel(colNames, 0); // (콜네임, 행의갯수).

		setBounds(100, 100, 450, 341);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getTextField());
		getContentPane().add(getBtnClear());

		for (int i = 0; i < 3; i++) {
			column = table.getColumnModel().getColumn(i);
			if (i == 2) {
				column.setPreferredWidth(202); // sport column is bigger
			} else {
				column.setPreferredWidth(68);
			}
		}

	}


	public void clear(){
		for(int i=table.getRowCount()-1;i>=0;i--){
			model.removeRow(i);
		}		
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 44, 420, 258);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		}
		return table;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("보기");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					SimpleDateFormat date2 = new SimpleDateFormat("yyyy-MM-dd");
					String temp2 = date2.format(new Date());
					
					model.setNumRows(0);
					Set<String> st2 = chulG.keySet();
					Iterator<String> it2 = st2.iterator();
					
					while (it2.hasNext()) {
						ChulGData cd = chulG.get(it2.next());
						if(cd.Datenal.contains(temp2)){
							String[] r = {cd.code, mem.get(cd.code).Irum, cd.Datenal,cd.ValueChul };
							model.addRow(r);
						}	
					}
					
					
					
					/*model.setNumRows(0);
					Set<String> st2 = chulG.keySet();
					Iterator<String> it2 = st2.iterator();
					while (it2.hasNext()) {
						ChulGData cd = chulG.get(it2.next());
						String[] r = {cd.code, mem.get(cd.code).Irum, cd.Datenal, cd.ValueChul };
						model.addRow(r);
					}
					
					if(table.getRowCount()==0){
						JOptionPane.showMessageDialog(null, "검색된 데이터가 없습니다.");
					}*/
				}
			});
			btnNewButton.setBounds(235, 13, 85, 21);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("조회");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(textField.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null, "공백을 채워주세요");
						return;
					}else{
						model.setNumRows(0);
						Set<String> st2 = chulG.keySet();
						Iterator<String> it2 = st2.iterator();

						while (it2.hasNext()) {
							ChulGData cd = chulG.get(it2.next());
							if(cd.code.contains(textField.getText()) 
									|| mem.get(cd.code).Irum.contains(textField.getText())){
								String[] r = {cd.code, mem.get(cd.code).Irum, cd.Datenal,
										cd.ValueChul };
								model.addRow(r);
							}
						}
					}					
				}
			});
			button.setBounds(138, 13, 85, 21);
		}
		return button;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(10, 13, 116, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnClear() {
		if (btnClear == null) {
			btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					clear();
				}
			});
			btnClear.setBounds(332, 13, 85, 21);
		}
		return btnClear;
	}
}
